package com.lanou.lenanapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.lanou.lenanapp.R;
import com.lanou.lenanapp.adapter.SpecialGutAdapter;
import com.lanou.lenanapp.beans.Special;
import com.lanou.lenanapp.beans.SpecialGut;
import com.lanou.lenanapp.volley.VolleySingleton;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/28.
 * 精彩专辑页面
 */
public class SpecialGutActivity extends Activity implements View.OnClickListener {

    private ImageView back_special_gut;
    VolleySingleton volleySingleton = VolleySingleton.getInstance();

    private RecyclerView specialimagerecyclerView;
    private SpecialGutAdapter specialGutAdapter;
    private GridLayoutManager manager;

    private String id;
    private List<Special> list ;
    private Special special;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_special_image);

        init();
        initData();

    }

    private void init() {
        special = new Special();
        list = new ArrayList<Special>();

        back_special_gut = (ImageView) findViewById(R.id.back_special_gut);
        back_special_gut.setOnClickListener(this);

        manager = new GridLayoutManager(this, 1);
        specialGutAdapter = new SpecialGutAdapter();
        specialimagerecyclerView = (RecyclerView) findViewById(R.id.specialimagerecyclerView);
        specialimagerecyclerView.setLayoutManager(manager);
        specialimagerecyclerView.setAdapter(specialGutAdapter);

        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        String url = intent.getStringExtra("aicon");
        String like = intent.getStringExtra("likes");
        special.setAname(title);
        special.setAicon(url);
        special.setLikes(like);
        list.add(special);

        id = intent.getStringExtra("aid");

    }

    private void initData() {

        String url = "http://120.55.151.67/weibofun/albums/album_content.php?apiver=10701&aid="+ id
                +"&page=0&page_size=20&get_album=0&platform=iphone&appver=1.9&buildver=1.9.4&udid=0FD678F7-07AA-4EBF-BB39-EB79EAB40753&sysver=7.0.4";
        JsonObjectRequest request = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.i("ge---", "list.size()" + "-----" + " specialGuts.size()");
                if (response != null && response.has("items")){
                    try {
                        JSONArray items = response.getJSONArray("items");
                        ArrayList<SpecialGut> specialGuts = new ArrayList<SpecialGut>();
                        Gson gson = new Gson();
                        for (int i = 0; i < items.length(); i ++){
                            String str = items.getString(i);
                            SpecialGut specialGut = gson.fromJson(str, SpecialGut.class);
                            specialGuts.add(specialGut);
                        }

                        specialGutAdapter.addData(specialGuts, list);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        volleySingleton.addRequest(request);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back_special_gut:
                this.finish();
                break;
        }
    }







}
